package com.ruoyi.admin.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.admin.domain.TPatientFamily;
import com.ruoyi.admin.mapper.FamilyMapper;
import com.ruoyi.admin.domain.Family;
import com.ruoyi.admin.service.IFamilyService;

/**
 * 家属信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-10
 */
@Service
public class FamilyServiceImpl implements IFamilyService 
{
    @Autowired
    private FamilyMapper familyMapper;

    /**
     * 查询家属信息
     * 
     * @param id 家属信息主键
     * @return 家属信息
     */
    @Override
    public Family selectFamilyById(Long id)
    {
        return familyMapper.selectFamilyById(id);
    }

    /**
     * 查询家属信息列表
     * 
     * @param family 家属信息
     * @return 家属信息
     */
    @Override
    public List<Family> selectFamilyList(Family family)
    {
        return familyMapper.selectFamilyList(family);
    }

    /**
     * 新增家属信息
     * 
     * @param family 家属信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFamily(Family family)
    {
        family.setCreateTime(DateUtils.getNowDate());
        int rows = familyMapper.insertFamily(family);
        insertTPatientFamily(family);
        return rows;
    }

    /**
     * 修改家属信息
     * 
     * @param family 家属信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFamily(Family family)
    {
        familyMapper.deleteTPatientFamilyByFamilyId(family.getId());
        insertTPatientFamily(family);
        return familyMapper.updateFamily(family);
    }

    /**
     * 批量删除家属信息
     * 
     * @param ids 需要删除的家属信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFamilyByIds(Long[] ids)
    {
        familyMapper.deleteTPatientFamilyByFamilyIds(ids);
        return familyMapper.deleteFamilyByIds(ids);
    }

    /**
     * 删除家属信息信息
     * 
     * @param id 家属信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFamilyById(Long id)
    {
        familyMapper.deleteTPatientFamilyByFamilyId(id);
        return familyMapper.deleteFamilyById(id);
    }

    /**
     * 新增查询关联家属信息
     * 
     * @param family 家属信息对象
     */
    public void insertTPatientFamily(Family family)
    {
        List<TPatientFamily> tPatientFamilyList = family.getTPatientFamilyList();
        Long id = family.getId();
        if (StringUtils.isNotNull(tPatientFamilyList))
        {
            List<TPatientFamily> list = new ArrayList<TPatientFamily>();
            for (TPatientFamily tPatientFamily : tPatientFamilyList)
            {
                tPatientFamily.setFamilyId(id);
                list.add(tPatientFamily);
            }
            if (list.size() > 0)
            {
                familyMapper.batchTPatientFamily(list);
            }
        }
    }
}
